var ports = []
let ws

self.onconnect = (e) => {
  const port = e.ports[0]
  ports.push(port)
  // 发送消息给连接的页面
  port.postMessage(
    JSON.stringify({
      type: 10,
      data: 'SharedWorker连接成功'
    })
  )
  console.log('SharedWorker连接成功')

  port.onmessage = (e) => {
    // 处理从连接的页面接收到的消息
    console.log('处理从连接的页面接收到的消息e', e)
    const d = e.data
    console.log('SharedWorker接收到的消息 : ' + d)
    // type=0 连接Socket
    // type=1 初始化Socket
    // type=2 发送数据
    // type=3 关闭连接
    // type=10 输出日志
    if (d.type == 0) {
      if (!ws) {
        const wsBaseUrl = d.data.wsBaseUrl
        try {
          postAllMessage({
            type: 10,
            data: 'WebSocket不存在，即将创建'
          })
          ws = new WebSocket(wsBaseUrl)
          postAllMessage({
            type: 0,
            success: true
          })
          postAllMessage({
            type: 10,
            data: 'WebSocket连接成功:' + wsBaseUrl
          })
        } catch (e) {
          postAllMessage({
            type: 0,
            success: false
          })
          postAllMessage({
            type: 10,
            data: 'WebSocket连接失败:' + wsBaseUrl + '\n错误信息：' + e
          })
        }
      } else {
        postAllMessage({
          type: 0,
          success: true
        })
        postAllMessage({
          type: 10,
          data: '沿用已有WebSocket连接成功'
        })
      }
    } else if (d.type == 1) {
      let s = setInterval(() => {
        if (WebSocket.OPEN == 1) {
          clearInterval(s)
          s = null
          console.log('WebSocket连接成功1')
          postAllMessage({
            type: 10,
            data: 'WebSocket连接成功1'
          })
          postAllMessage({
            type: 1,
            success: true,
            method: 'onopen'
          })
        }
      }, 1000)

      ws.onclose = (e) => {
        postAllMessage({
          type: 10,
          data: 'WebSocket连接关闭'
        })
        postAllMessage({
          type: 1,
          success: true,
          method: 'onclose',
          data: e
        })
      }
      ws.onmessage = (e) => {
        const data = e.data
        postAllMessage({
          type: 10,
          data: 'WebSocket获取到数据'
        })
        postAllMessage({
          type: 1,
          success: true,
          method: 'onmessage',
          data: data
        })
      }
      ws.onerror = (e) => {
        postAllMessage({
          type: 10,
          data: 'WebSocket连接错误'
        })
        postAllMessage({
          type: 1,
          success: true,
          method: 'onerror',
          data: e
        })
      }
    } else if (d.type == 2) {
      console.log('WebSocket发送数据', d.data)
      ws.send(d.data)
      postAllMessage({
        type: 10,
        data: 'WebSocket发送数据成功'
      })
    } else if (d.type == 3) {
      ws.close()
      postAllMessage({
        type: 10,
        data: 'WebSocket关闭连接成功'
      })
    }
  }

  function postAllMessage(msg) {
    console.log('ports', ports)
    console.log('typeof msg', typeof msg)
    // 广播消息给所有连接的页面
    for (let i = 0; i < ports.length; i++) {
      ports[i].postMessage(
        JSON.stringify({
          type: 10,
          data: 'postAllMessage'
        })
      )
      const message = JSON.stringify(msg)
      console.log('message', message)
      ports[i].postMessage(message)
    }
  }
}
